﻿var storage = {};
storage.acmers = {};
storage.acmers.list = [];
storage.acmers.status = "ready";
storage.upload = function(){
	//update local storage
	;
}
storage.acmers.save_user_list = function(){
	try{
		localStorage['acmers_list'] = JSON.stringify(storage.acmers.list);
	}catch(e){
		console.log(e);
	}	
}
storage.acmers.get_user_list = function(g){
	var list;
	try{
		list = JSON.parse(localStorage['acmers_list']);
		if (!(list.length >= 0)){
			storage.acmers.list = [];
			console.log('localStorage error');
		}else{
			storage.acmers.list = [];
			for (var i = 0;i < list.length;i++){
				storage.acmers.list[i] = new acmer();
				storage.acmers.list[i].name = list[i].name;
				storage.acmers.list[i].uid = list[i].uid;
				storage.acmers.list[i].oj = list[i].oj;
				storage.acmers.list[i].updatetime = list[i].updatetime;
				if (g != "init")
					storage.acmers.list[i].status = list[i].status;
				else{
					for (site in storage.acmers.list[i].oj){
						storage.acmers.list[i].oj[site].status = "ready";
						storage.acmers.list[i].oj[site].updatetime = 0;
					}
				}
			}
		}		
	}catch(e){
		console.log(e);
	}
}

storage.acmers.sort = function(){
	;
}
storage.acmers.add = function(name,hdu_id,pku_id,bistu_id){
	var newacmer = new acmer();
	
	if (typeof name != "string" || !name)
		return '用户名格式错误';
	newacmer.name = name;
	
	if (typeof hdu_id == 'undefined')
		hdu_id = "";
	newacmer.oj.hdu.id = hdu_id;
		
	if (typeof pku_id == 'undefined')
		pku_id = "";
	newacmer.oj.pku.id = pku_id;

	if (typeof bistu_id == 'undefined')
		bistu_id = "";
	newacmer.oj.bistu.id = bistu_id;
		
	
	storage.acmers.list.push(newacmer);	
	//storage.acmers.save_user_list();
	return '添加成功';
}
storage.acmers.del = function(uid){
	var i;
	for (i = 0;i < storage.acmers.list.length;i++){
		if (storage.acmers.list[i].uid == uid){
			storage.acmers.list.splice(i,1);
			storage.acmers.save_user_list();
			return '删除成功';
		}
	}
	return '不存在此用户';
}
storage.acmers.edit = function(uid){
	;
}

storage.acmers.update_oj = function(){
	var id = 0;
	var process = 0;
	var status = [0,0,0];
	var oj_site = ['hdu','pku','bistu'];
	var i = 0;
	storage.acmers.status = "loading";
	var timer = setInterval(function(){
		if (id >= storage.acmers.list.length){
			clearInterval(timer);
			storage.acmers.status = "finish";
			chrome.extension.sendRequest({"hope": "refresh_page_rank"}, function(response) {
				;	
			});
			return ;
		}
		for (i = 0;i < 3;i++){
			if (status[i] == 0){
				if (new Date().getTime() -storage.acmers.list[id].oj[oj_site[i]].updatetime >= 600000 && storage.acmers.list[id].oj[oj_site[i]].status != "success"){
					storage.acmers.list[id].update(oj_site[i]);
					console.log('更新'+id+storage.acmers.list[id].name+oj_site[i]+'...开始');
					status[i] = 2;	
				}else{
					console.log('更新'+id+storage.acmers.list[id].name+oj_site[i]+'...跳过');
					status[i] = 1;	
					
				}			
			}	
			if (status[i] != 1 && (storage.acmers.list[id].oj[oj_site[i]].status == "failed" || storage.acmers.list[id].oj[oj_site[i]].status == "success")){
				console.log('更新'+id+storage.acmers.list[id].name+oj_site[i]+'...'+storage.acmers.list[id].oj[oj_site[i]].status);
				status[i] = 1;
			}
			
		}
		/*
		if (status[1] == 0){
			storage.acmers.list[id].update('pku');
			console.log('更新'+storage.acmers.list[id].name+'PKU...开始');
			status[1] = 2;	
		}	
		if (status[2] == 0){
			storage.acmers.list[id].update('bistu');
			console.log('更新'+storage.acmers.list[id].name+'BISTU...开始');
			status[2] = 2;	
		}	
		
		if (status[1] != 1 && (storage.acmers.list[id].oj.pku.status == "failed" || storage.acmers.list[id].oj.pku.status == "success")){
			console.log('更新'+id+storage.acmers.list[id].name+'PKU...'+storage.acmers.list[id].oj.pku.status);
			status[1] = 1;
		}	
		if (status[2] != 1 && (storage.acmers.list[id].oj.bistu.status == "failed" || storage.acmers.list[id].oj.bistu.status == "success")){
			console.log('更新'+id+storage.acmers.list[id].name+'BISTU...'+storage.acmers.list[id].oj.bistu.status);
			status[2] = 1;
		}	
		*/
		if (status[0] == 1 && status[1] == 1 && status[2] == 1){
			status = [0,0,0];
			chrome.extension.sendRequest({"hope": "refresh_page_rank"}, function(response) {
				;	
			});
			id++;
		}	
			
	},500);
}

